//
//  NetworkManager.m
//  OralRobot
//
//  Created by 王桂敏 on 16/3/25.
//  Copyright © 2016年 王桂敏. All rights reserved.
//

#import "NetworkManager.h"
#import "NSString+secure.h"
#import "JSONKit.h"


@interface NetworkManager ()<NetworkingHandleDelegate>

@end

@implementation NetworkManager

- (NetworkingHandle *)handle{
    if (!_handle) {
        self.handle = [[NetworkingHandle alloc] init];
        
    }
    return _handle;
}

+ (id)manager{
    NetworkManager *manager = [[NetworkManager alloc] init];
    return manager;
}

/*

- (instancetype)init{
    self = [super init];
    if (self) {
        [self configAFHTTPSessionManager];
    }
    return self;
}

- (void)configAFHTTPSessionManager{

    self.handle.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html",nil];
    self.handle.manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [self.handle.manager.requestSerializer setValue:@"VGFrTFG1P28qvEFEeKvyuRlo-gzGzoHsz" forHTTPHeaderField:@"X-LC-Id"];
    
    [self.handle.manager.requestSerializer setValue:@"tvghBYHgukohjovoy4GtdcOV" forHTTPHeaderField:@"X-LC-Key"];
    [self.handle.manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
}
 */

- (NSDictionary *)configParam:(NSMutableDictionary *)param isEncrypt:(BOOL)isEncrypt{
    if (!param) {
        param = [NSMutableDictionary dictionary];
    }
    
    [param addEntriesFromDictionary:@{@"terminalType":@"ios",@"token":self.token,@"userId":self.userID,@"nowTime":self.backgroundTime,@"sysVersion":[ApplyUntil version]}];
    
    if (isEncrypt) {
        
        NSMutableString *paramStr = [NSMutableString stringWithFormat:@"%@",[param JSONString]];
        
        return @{@"content":[paramStr encoder]};
    } else {
        
        return param;
    }
    
}


- (NSString *)configURL:(NSString *)URL{
    NSString *realURL = [NSString stringWithFormat:@"%@%@",Baseurl,URL];
    return realURL;
}
- (void)postWithURL:(NSString *)URL
              param:(NSMutableDictionary *)param
           delegate:(id<NetworkingHandleDelegate>)delegate
          isEncrypt:(BOOL)isEncrypt
               type:(NetWorkingType)type{
    
    NSString *realURL = [self configURL:URL];
    
   NSDictionary *paramDic = [self configParam:param isEncrypt:isEncrypt];
    
    [self.handle postNetworkingWithURL:realURL parameters:paramDic type:type delegate:delegate];
    
}


- (void)postWithURL:(NSString *)URL
       imagePicName:(NSString *)imagePicName
         imageArray:(NSArray *)imageArray
              param:(NSMutableDictionary *)param
           delegate:(id<NetworkingHandleDelegate>)delegate
          isEncrypt:(BOOL)isEncrypt
               type:(NetWorkingType)type{

    NSString *realURL = [self configURL:URL];
    
    NSDictionary *paramDic = [self configParam:param isEncrypt:isEncrypt];
    [self.handle postNetworkingWithURL:realURL imagePicName:imagePicName imageArray:imageArray parameters:paramDic type:type delegate:delegate];
    
}

- (void)postWithURL:(NSString *)URL
       imagePicName:(NSString *)imagePicName
              image:(UIImage *)image
              param:(NSMutableDictionary *)param
           delegate:(id<NetworkingHandleDelegate>)delegate
          isEncrypt:(BOOL)isEncrypt
               type:(NetWorkingType)type{
    NSString *realURL = [self configURL:URL];
    
    NSDictionary *paramDic = [self configParam:param isEncrypt:isEncrypt];
    [self.handle postNetworkingWithURL:realURL imagePicName:imagePicName image:image parameters:paramDic type:type delegate:delegate];
    
}
//  临时:全地址请求
- (void)postTestWithURL:(NSString *)URL
                  param:(NSMutableDictionary *)param
               delegate:(id<NetworkingHandleDelegate>)delegate
              isEncrypt:(BOOL)isEncrypt
                   type:(NetWorkingType)type{
    
    NSDictionary *paramDic = [self configParam:param isEncrypt:isEncrypt];
    
    [self.handle postNetworkingWithURL:URL parameters:paramDic type:type delegate:delegate];
    
}

@end
